#!/bin/bash

# 设置升级选项
upgrade_options=("升级全部" "升级oa" "升级blog" "升级dev" "还原升级后数据" "还原升级前数据" "还原上次19:30备份数据" "还原其它机器备份的数据" "取消升级还原")

# 配置文件路径
CONFIG_FILE="/opt/gougu/user_config.txt"

# 预定义的用户名和对应的 SHA-256 加密后的密码
declare -A USERS=(
    ["dsm100"]="230c1627def5bb3fd899ee4882c4b3062515b87c1db7afb0bc9ce2574981fde5"  # 
    ["oa101"]="e9957ad4bb51a797fed2ec67d12cee01303852e91395d4c014972bb1016a867e"  # 

)

# 函数：执行升级
execute_upgrade() {
    local markers=("$@")
    URL="https://gitee.com/banban1000/docker/raw/master/oa/allupdate.sh"
    TEMP_FILE=$(mktemp)
    EXTRACTED_FILE="${TEMP_FILE}.extracted"

    > "$EXTRACTED_FILE"

    for ((i = 0; i < ${#markers[@]}; i += 2)); do
        start_marker=${markers[i]}
        end_marker=${markers[i+1]}
        curl -fsSL "$URL" | sed -n -e "/$start_marker/,/$end_marker/p" >> "$EXTRACTED_FILE"
    done

    if [ -s "$EXTRACTED_FILE" ]; then
        bash "$EXTRACTED_FILE"
    else
        echo "未提取到指定标记之间的内容。"
    fi

    rm -f "$TEMP_FILE" "$EXTRACTED_FILE"
}

# 函数：验证用户和密码
authenticate() {
    local username=$1
    local password=$2

    if [[ -z "${USERS[$username]}" ]]; then
        echo "用户名不存在。"
        return 1
    fi

    input_password_hash=$(echo -n "$password" | sha256sum | awk '{print $1}')

    if [[ "${USERS[$username]}" == "$input_password_hash" ]]; then
        echo "验证成功！欢迎, $username。"
        return 0
    else
        echo "密码错误。"
        return 1
    fi
}

# 函数：从配置文件加载账号密码
load_credentials() {
    if [[ -f "$CONFIG_FILE" ]]; then
        local saved_username=$(head -n 1 "$CONFIG_FILE")
        local saved_password_hash=$(tail -n 1 "$CONFIG_FILE")

        if [[ -n "${USERS[$saved_username]}" && "${USERS[$saved_username]}" == "$saved_password_hash" ]]; then
            echo "检测到已保存的账号密码，自动登录中..."
            return 0
        else
            echo "配置文件中的账号密码无效，请重新输入。"
            rm -f "$CONFIG_FILE"
            return 1
        fi
    else
        echo "未检测到配置文件，需要输入账号密码。"
        return 1
    fi
}

# 函数：保存账号密码到配置文件
save_credentials() {
    local username=$1
    local password=$2

    # 计算密码的哈希值
    password_hash=$(echo -n "$password" | sha256sum | awk '{print $1}')

    mkdir -p "$(dirname "$CONFIG_FILE")"
    echo "$username" > "$CONFIG_FILE"
    echo "$password_hash" >> "$CONFIG_FILE"
    echo "账号密码已保存到配置文件。"
}

# 主逻辑
if ! load_credentials; then
    read -p "请输入用户名: " username
    read -p "请输入密码: " password
    echo

    if authenticate "$username" "$password"; then
        save_credentials "$username" "$password"
    else
        echo "验证失败，退出脚本。"
        exit 1
    fi
fi

# 显示升级选项
index=1
for option in "${upgrade_options[@]}"; do
    echo "$index) $option"
    index=$((index+1))
done

# 设置计时器开始时间
start_time=$(date +%s)

# 等待用户输入
echo "请60秒内选择上面对应升级选项,不选择则自动全部升级."
read -t 60 -p "Selection: " user_choice

# 60秒内没有输入，执行自动全部升级
if [ -z "$user_choice" ]; then
    echo "60秒内没有选择，升级全部中......"
    execute_upgrade "start1" "end1" "start2" "end2" "start3" "end3" "start4" "end4" "start5" "end5"
else
    case $user_choice in
        1)
            echo "升级全部......."
            execute_upgrade "start1" "end1" "start2" "end2" "start3" "end3" "start4" "end4" "start5" "end5"
            ;;
        2)
            echo "升级oa......."
            execute_upgrade "start1" "end1" "start2" "end2" "start5" "end5"
            ;;
        3)
            echo "升级blog......"
            execute_upgrade "start1" "end1" "start3" "end3" "start5" "end5"
            ;;
        4)
            echo "升级dev......"
            execute_upgrade "start1" "end1" "start4" "end4" "start5" "end5"
            ;;
        5)
            echo "还原升级后数据中......"
            execute_upgrade "start6" "end6"
            ;;
        6)
            echo "还原升级前数据中......"
            execute_upgrade "start7" "end7"
            ;;
        7)
            echo "还原上次19:30备份数据中......"
            execute_upgrade "start8" "end8"
            ;;
        8)
            echo "还原其它机器备份的数据中......"
            execute_upgrade "start9" "end9"
            ;;
        *)
            echo "无效的选择."
            exit 1
            ;;
    esac
fi

exit 0